package com.sls.web.manager.sys.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.common.KeyUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;
import com.sls.web.manager.sys.pojo.OperatorVO;
import com.sls.web.manager.sys.pojo.Role;
import com.sls.web.manager.sys.querybean.OperatorQuery;
import com.sls.web.manager.sys.service.OperatorService;

@Service("operatorService")
public class OperatorServiceImpl extends SuperService implements
		OperatorService {

	@Override
	public boolean regiest(OperatorVO operator, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		operator.setPwd(KeyUtil.passwordEncrypt(operator.getPwd()));
		head.setServiceCode(Constants.REGIEST);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, operator);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());

	}

	@Override
	public List<Role> queryrole(UserInfoVO userinfo) {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setServiceCode(Constants.QUERY_ROLE);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head);
		List<Role> retList = gson.fromJson(dto.getBody(),
				new TypeToken<List<Role>>() {
				}.getType());
		return retList;
	}

	@Override
	public OperatorVO login(OperatorVO operator, String ip) throws Exception {
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.LOGIN);
		operator.setPwd(KeyUtil.passwordEncrypt(operator.getPwd()));
		head.setUserId(operator.getAccount());
		SocketDTO dto = super.send(head, operator);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			Gson gson = CommonUtil.gson();
			return gson.fromJson(dto.getBody(), OperatorVO.class);
		} else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public boolean updatepwd(OperatorVO pwd, UserInfoVO userinfo) throws Exception {
		String ip = userinfo.getCurrentIp();
		pwd.setNewPwd(KeyUtil.passwordEncrypt(pwd.getNewPwd()));
		pwd.setPwd(KeyUtil.passwordEncrypt(pwd.getPwd()));
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.UPDATE_PWD);
		pwd.setAccount(userinfo.getUserName());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, pwd);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public Pagination<?> queryOperator(OperatorQuery rQuery, UserInfoVO userinfo)
			throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_OPERATOR);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, rQuery);
		String body = dto.getBody();
		Pagination<OperatorVO> s = gson.fromJson(body,
				new TypeToken<Pagination<OperatorVO>>() {
				}.getType());

		return s;
	}

	@Override
	public boolean updateUser(OperatorVO operator, UserInfoVO userinfo)
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.UPDATE_USER);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, operator);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public boolean cancelUser(OperatorVO operator, UserInfoVO userinfo) 
			throws Exception {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.UPDATE_STATE);
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, operator);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public OperatorVO queryUserInfoById(String id,
			UserInfoVO userInfoVO) throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_USER_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		
		head.setUserId(userInfoVO.getUserId());
		OperatorVO op = new OperatorVO();
		op.setId(id);
		SocketDTO dto = super.send(head, op);
		String body = dto.getBody();
		OperatorVO operator = gson.fromJson(body, OperatorVO.class);
		return operator;
	}

	@Override
	public boolean checkUsername(OperatorVO operator, UserInfoVO userinfo) {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.CHECKUSERNAME);
		head.setUserId(userinfo.getUserId());
		operator.setAccount(operator.getAccount());
		SocketDTO dto = super.send(head, operator);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			return false;
	}
}
